< previous page page_158 next page >

Page 158
Solution 4
It's All in the Name
How can a simple little piece of code cause so many problems?
Private Declare Function GetComputerName Lib "kernel32" (ByVal _
ComputerName As String, ByVal BufferSize As Long) As Long

Private Const MAX_COMPUTERNAME_LENGTH = 15

Private Sub Form_Load()
   Dim s$
   Call GetComputerName(s$, MAX_COMPUTERNAME_LENGTH + 1)
   lblName.Caption = s$
End Sub
The approach for fixing a program like this is the same regardless of complexity. You try something, interpret the errors that occur, and modify the code to fix the error. Then you move on to the next error.
The DLL Entry Point
When you run this program, you get runtime error 453: Can't find DLL entry point GetComputerName in kernel32.
If you've looked at Tutorial 1, "Finding Functions" (in Part III of this book), you know that this error means that the requested API function does not exist in the specified DLL. Your first reaction might be to think that the GetComputerName function must be in a different DLL. But before you start searching, note that this function has a string parameter. In most cases, functions that take string parameters actually have two entry points: the ANSI entry point for single-byte characters and the Unicode entry point for double-byte characters.
Try changing the declaration as follows:
Private Declare Function GetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal ComputerName As String, ByVal BufferSize _
As Long) As Long
This solves the problem. Runtime error 453 vanishes as the function is found.

 
< previous page page_158 next page >